package com.zuihai.app.mvp.ui.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.zuihai.app.R;

/**
 * Created by Administrator on 2017/5/17 0017.
 */

public class InputView extends LinearLayout {

    private ImageView mPlusView;
    private ImageView mMinusView;
    private TextView mInputView;
    private int mCount = 1;
    private OnInputChangeListener mListener;
    private int mMaxCount = 360;

    public InputView(Context context) {
        this(context, null);
    }

    public InputView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        setOrientation(HORIZONTAL);
        initMinus();
        initEdit();
        initPlus();
    }

    private void initPlus(){
        int size = ConvertUtils.dp2px(45);
        LayoutParams layoutParams = new LayoutParams(size, size);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        mPlusView = new ImageView(getContext());
        mPlusView.setBackgroundResource(R.mipmap.button_increase);
        mPlusView.setLayoutParams(layoutParams);
        mPlusView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setCount(++mCount);
            }
        });
        addView(mPlusView);
    }

    private void initMinus(){
        int size = ConvertUtils.dp2px(45);
        LayoutParams layoutParams = new LayoutParams(size, size);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        mMinusView = new ImageView(getContext());
        mMinusView.setBackgroundResource(R.mipmap.button_reduce);
        mMinusView.setLayoutParams(layoutParams);
        mMinusView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setCount(--mCount);
            }
        });
        addView(mMinusView);
    }

    private void initEdit(){
        LayoutParams layoutParams = new LayoutParams(ConvertUtils.dp2px(50), ConvertUtils.dp2px(45));
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        layoutParams.rightMargin = ConvertUtils.dp2px(5);
        layoutParams.leftMargin = ConvertUtils.dp2px(5);
        mInputView = new TextView(getContext());
        mInputView.setSingleLine();
        mInputView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(4)});
        mInputView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        mInputView.setTextColor(getContext().getResources().getColor(R.color.color_recomment_day_text));
        mInputView.setGravity(Gravity.CENTER);
        mInputView.setBackgroundResource(R.drawable.detail_input_edit_shape);
        mInputView.setText(mCount + "");
        mInputView.setLayoutParams(layoutParams);
        addView(mInputView);
    }

    private void setCount(int count){
        if (mCount >= 1 && mCount <= mMaxCount){
            mCount = count;
            mInputView.setText(String.valueOf(mCount));
        }else{
            mCount = Integer.valueOf(mInputView.getText().toString());
        }
        if (mListener != null){
            mListener.onChange(mCount);
        }
    }

    public int getCount(){
        return mCount;
    }

    public void setMaxCount(int maxCount){
        mMaxCount = maxCount;
    }

    public void setOnInputChangeListener(OnInputChangeListener listener){
        this.mListener = listener;
    }

    public interface OnInputChangeListener{
        void onChange(int value);
    }
}
